package yunwo.cn.yzstation.model;

import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import yunwo.cn.yzstation.api.HttpManager;
import yunwo.cn.yzstation.api.IscanAssigModel;
import yunwo.cn.yzstation.api.okapi.GLCallBack;
import yunwo.cn.yzstation.bean.AssigmnetBean;
import yunwo.cn.yzstation.bean.ContinueBean;
import yunwo.cn.yzstation.bean.ItemlistBean;
import yunwo.cn.yzstation.config.MyConstants;
import yunwo.cn.yzstation.presenter.ScanAssigPersenter;
import yunwo.cn.yzstation.utils.StringUtil;
import yunwo.cn.yzstation.view.activity.ScanAssigAcivity;

/**
 * Created by lao on 2017/10/13.
 */
public class ScanAssigModel {
    ScanAssigAcivity scanAssigAcivity;
    IscanAssigModel iscanAssigModel;

    public ScanAssigModel(ScanAssigAcivity scanAssigAcivity, IscanAssigModel iscanAssigModel) {
        this.scanAssigAcivity = scanAssigAcivity;
        this.iscanAssigModel = iscanAssigModel;
    }

    public void getoBtainTrack() {
        Map<String, Object> map = new HashMap<>();

        map.put("beginOrganId", "");
        map.put("status", 4);

        HttpManager.hpptPostString3(MyConstants.obtainTrack, map).execute(new GLCallBack() {

            private List<ItemlistBean.DataBean> mDataBeanList;

            @Override
            public void onResponseT(String result, int code, String msg, int id) {
                if (code == 200) {
                    if (!StringUtil.isEmpty(result)) {
                        mDataBeanList = new Gson().fromJson(result, new TypeToken<List<ItemlistBean.DataBean>>() {
                        }.getType());
                        iscanAssigModel.getoBtainTrackSueecss(mDataBeanList);
                    }
                } else {
                    Toast.makeText(scanAssigAcivity, msg, Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                Toast.makeText(scanAssigAcivity, "出错了", Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void complete(AssigmnetBean assigmentBean,
                         ContinueBean.DataBean mContinueBean,
                         List<ItemlistBean.DataBean> selectedCollectedCars) {
        Map<String, Object> map = new HashMap<>();

        if (mContinueBean != null) {
            map.put("batchNumber", mContinueBean.getBatchNumber());
            map.put("departDetailList", selectedCollectedCars);
            map.put("id", mContinueBean.getDepartId());
            map.put("driverName", mContinueBean.getDriverName());
            map.put("driverPhone", mContinueBean.getDriverPhone());
            map.put("isShort", 1);
            map.put("plateNumber", mContinueBean.getPlateNumber());
            map.put("vehicleType", mContinueBean.getVehicleType());
            map.put("truckTarget", mContinueBean.getTruckTarget());
            map.put("truckTargetId", mContinueBean.getTruckTargetId());
        }else {
            map.put("batchNumber", assigmentBean.getBatch());
            map.put("departDetailList", selectedCollectedCars);
//        map.put("id", mMContinueBean.getDepartId());
            map.put("driverId", assigmentBean.getDriverId());
            map.put("driverName", assigmentBean.getDriver());
            map.put("driverPhone", assigmentBean.getDriverphone());
            map.put("isShort", 1);
            map.put("plateNumber", assigmentBean.getCarnumber());
            map.put("vehicleId", assigmentBean.getCarId());
            map.put("vehicleType", assigmentBean.getCartypeId());
            map.put("truckTarget", assigmentBean.getCarobj());
            map.put("truckTargetId", assigmentBean.getCarobjId());
        }
        HttpManager.hpptPostString3(MyConstants.confirmPz, map).execute(new GLCallBack() {
            @Override
            public void onResponseT(String result, int code, String msg, int id) {
                if (code == 200) {
                    iscanAssigModel.completeSueescc();
                } else {
                    Toast.makeText(scanAssigAcivity, msg, Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onError(Call call, Exception e, int id) {
                Toast.makeText(scanAssigAcivity, "网络出错了", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
